<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

function get_list_game($type = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid as nid, ';
		$str_query .= ' a.cgame	 		as 		cgame, ';
		$str_query .= ' a.cimage	 	as 		cimage, ';
		$str_query .= ' b.ctournament	as 		ctournament, ';
		$str_query .= ' a.ffee			as 		ffee, ';
		$str_query .= ' a.npermission	as 		npermission ';
		$str_query .= ' FROM '.Fget_ap_table('tgame_list').' as a ';
		$str_query .= ' LEFT JOIN '.Fget_ap_table('ttournament').' as b ON (a.nid_tournament = b.nid)';
		$str_query .= ' WHERE a.nid is not null ';
		if($type != '')
			$str_query .= ' AND ntype = '.$type;
		$str_query .= ' AND a.nstatus = 1 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function count_number_attach_single_game($nid_game)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_result = $obj_helper->db->get('tuser_single_game');
		return $obj_result->num_rows();
	}

function check_user_registry_single_game($nid_game, $nid_gamer)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->group_by(array("nid_gamer", "nid_game_list"));
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_helper->db->where('nid_gamer', $nid_gamer);
		$obj_result = $obj_helper->db->get('tuser_single_game');
		return $obj_result->num_rows();
	}

function count_number_attach_team_game($nid_game)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->group_by(array("nid_gamer", "nid_game_list"));
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_result = $obj_helper->db->get('tuser_team_game');
		return $obj_result->num_rows();
	}

function check_user_registry_team_game($nid_game, $nid_gamer)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->group_by(array("nid_gamer", "nid_game_list"));
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_helper->db->where('nid_gamer', $nid_gamer);
		$obj_result = $obj_helper->db->get('tuser_team_game');
		return $obj_result->num_rows();
	}

function get_list_game_byid($nid = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid as nid, ';
		$str_query .= ' a.cgame	 		as 		cgame, ';
		$str_query .= ' a.cimage	 	as 		cimage, ';
		$str_query .= ' b.ctournament	as 		ctournament, ';
		$str_query .= ' a.ffee			as 		ffee ';
		$str_query .= ' FROM '.Fget_ap_table('tgame_list').' as a ';
		$str_query .= ' LEFT JOIN '.Fget_ap_table('ttournament').' as b ON (a.nid_tournament = b.nid)';
		$str_query .= ' WHERE a.nid is not null ';
		if($nid != '')
			$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_gamer_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tgamer').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_country_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tcountry').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_my_list_single_game($nid_gamer)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid as nid, a.cgame as cgame, a.nid_tournament as nid_tournament, d.ctournament as ctournament, a.cimage as cimage, a.ffee as ffee, c.nstatus_payment as nstatus_payment, c.ddate01 as ddate01, c.npayment as npayment   ';
		$str_query .= ' FROM '.Fget_ap_table('tgame_list').' as a, ';
		$str_query .= ' '.Fget_ap_table('tgamer').' as b, ';
		$str_query .= ' '.Fget_ap_table('tuser_single_game').' as c, ';
		$str_query .= ' '.Fget_ap_table('ttournament').' as d ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.ntype = 1';
		$str_query .= ' AND b.nid = '.$nid_gamer;
		$str_query .= ' AND b.nid = c.nid_gamer ';
		$str_query .= ' AND a.nid = c.nid_game_list ';
		$str_query .= ' AND a.nid_tournament = d.nid ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}


function get_my_list_team_game($nid_gamer)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid as nid, a.cgame as cgame, a.nid_tournament as nid_tournament, d.ctournament as ctournament, a.cimage as cimage, a.ffee as ffee, c.nstatus_payment as nstatus_payment, c.ddate01 as ddate01, c.npayment as npayment   ';
		$str_query .= ' FROM '.Fget_ap_table('tgame_list').' as a, ';
		$str_query .= ' '.Fget_ap_table('tgamer').' as b, ';
		$str_query .= ' '.Fget_ap_table('tuser_team_game').' as c, ';
		$str_query .= ' '.Fget_ap_table('ttournament').' as d ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.ntype = 2 ';
		$str_query .= ' AND b.nid = '.$nid_gamer;
		$str_query .= ' AND b.nid = c.nid_gamer ';
		$str_query .= ' AND a.nid = c.nid_game_list ';
		$str_query .= ' AND a.nid_tournament = d.nid ';
		$str_query .= ' GROUP BY nid_gamer, nid_game_list ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_status_payment($value)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tstatus').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.cvalue = '.$value;
		$str_query .= ' AND a.cgroup = 1 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}

function get_list_team_game_byid($nid_game = '', $nid_user = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tuser_team_game').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_gamer = '.$nid_user;
		$str_query .= ' AND a.nid_game_list = '.$nid_game;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function count_payment_team_game($nid_game,$npayment)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->group_by(array("nid_gamer", "nid_game_list"));
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_helper->db->where('npayment', $npayment);
		$obj_result = $obj_helper->db->get('tuser_team_game');
		return $obj_result->num_rows();
	}

function count_payment_complete_team_game($nid_game,$npayment)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->group_by(array("nid_gamer", "nid_game_list"));
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_helper->db->where('npayment', $npayment);
		$obj_helper->db->where('nstatus_payment', 2);
		$obj_result = $obj_helper->db->get('tuser_team_game');
		return $obj_result->num_rows();
	}
	
function count_payment_single_game($nid_game,$npayment)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_helper->db->where('npayment', $npayment);
		$obj_result = $obj_helper->db->get('tuser_single_game');
		return $obj_result->num_rows();
	}

function count_payment_complete_single_game($nid_game,$npayment)
	{
		$obj_helper =& get_instance(); 	
		$obj_helper->db->where('nid_game_list', $nid_game);
		$obj_helper->db->where('npayment', $npayment);
		$obj_helper->db->where('nstatus_payment', 2);
		$obj_result = $obj_helper->db->get('tuser_single_game');
		return $obj_result->num_rows();
	}

function Obj_get_country() 
	{ 
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();
		$obj_result 	= $obj_helper->db->get(Fget_ap_table('tcountry')); 
		$obj_results 	= $obj_result->result_array();
		return 	$obj_results;
	}